package leetcode.od;

import org.junit.Test;

import java.util.Collections;
import java.util.HashMap;

/**
 * @author pppppp
 * @date 2022/3/30 10:52
 * 骰子是一个正方体，每个面有一个数字，初始为左1，右2，前3，后4，上5，下6，用123456表示这个状态，
 * 放置在平面上，可以向左翻转（用L表示向左翻转1次）；可以向右翻转（用R表示向右翻转1次）；
 * 可以向前翻转（用F表示向前翻转1次）；可以向后翻转（用B表示向后翻转1次）；
 * 可以逆时针翻转（用A表示向逆时针翻转1次）；可以向顺时针翻转（用C表示向顺时针翻转1次）
 * 输入一行指令，输出最后骰子的状态
 * <p>
 * 输入
 * RL
 * 输出
 * 123456
 * 解释：骰子向右转一下，再向左转一下，各面结果相当于不变
 */
public class 转骰子 {
    @Test
    public void T_0() {
        String[] s = {"RLFBAC","RL","L","R","F","B","A","C"};
        String[] ans = {"123456","123456","563421","653412","125643","126534","342156","431256"};
        for (int i = 0; i < s.length; i++) {
            System.out.println(solution(s[i]).equals(ans[i]));
        }
    }

    private String solution(String s) {
        char[] charArray = s.toCharArray();
        HashMap<Character, Integer> map = new HashMap<>();
        for (char c : charArray) {
            if (c == 'L' || c == 'R') {
                int i = c == 'L' ? 1 : -1;
                map.put('L', map.getOrDefault('L', 0) + i);
            } else if (c == 'F' || c == 'B') {
                int i = c == 'F' ? 1 : -1;
                map.put('F', map.getOrDefault('F', 0) + i);
            } else if (c == 'A' || c == 'C') {
                int i = c == 'A' ? 1 : -1;
                map.put('A', map.getOrDefault('A', 0) + i);
            }
        }

        int[] times = new int[6];
        if (map.get('L') != null) {
            if (map.get('L') > 0) {
                times[0] = map.get('L') % 4;
            } else if (map.get('L') < 0) {
                times[1] = (-map.get('L')) % 4;
            }
        }

        if (map.get('F') != null) {
            if (map.get('F') > 0) {
                times[2] = map.get('F') % 4;
            } else if (map.get('F') < 0) {
                times[3] = (-map.get('F')) % 4;
            }
        }

        if (map.get('A') != null) {
            if (map.get('A') > 0) {
                times[4] = map.get('A') % 4;
            } else if (map.get('A') < 0) {
                times[5] = (-map.get('A')) % 4;
            }
        }

        String[] base = {"1", "2", "3", "4", "5", "6"};
        for (int i = 0; i < times.length; i++) {
            if (times[i] != 0) {
                swap(base, i, times[i]);
            }
        }
        String res = "";
        for (String s1 : base) {
            res += s1;
        }
        return res;
    }

    private void swap(String[] base, int i, int time) {
        while (time > 0) {
            switch (i) {
                case 0:
                    swapS(base, 0, 4);
                    swapS(base, 1, 5);
                    swapS(base, 4, 5);
                    break;
                case 1:
                    swapS(base, 0, 4);
                    swapS(base, 1, 5);
                    swapS(base, 0, 1);
                    break;
                case 2:
                    swapS(base,2,4);
                    swapS(base,3,5);
                    swapS(base,4,5);
                    break;
                case 3:
                    swapS(base,2,4);
                    swapS(base,3,5);
                    swapS(base,2,3);
                    break;
                case 4:
                    swapS(base,0,2);
                    swapS(base,1,3);
                    swapS(base,2,3);
                    break;
                case 5:
                    swapS(base,0,2);
                    swapS(base,1,3);
                    swapS(base,0,1);
                    break;
            }
            time--;
        }
    }

    private void swapS(String[] base, int i, int i1) {
        String s = base[i];
        base[i] = base[i1];
        base[i1] = s;
    }
}
